package com.liwalla.domain;

import com.liwalla.domain.Kid;
import com.liwalla.domain.ParentDataOnDemand;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect KidDataOnDemand_Roo_DataOnDemand {
    
    declare @type: KidDataOnDemand: @Component;
    
    private Random KidDataOnDemand.rnd = new java.security.SecureRandom();
    
    private List<Kid> KidDataOnDemand.data;
    
    @Autowired
    private ParentDataOnDemand KidDataOnDemand.parentDataOnDemand;
    
    public Kid KidDataOnDemand.getNewTransientKid(int index) {
        com.liwalla.domain.Kid obj = new com.liwalla.domain.Kid();
        obj.setBirthCity(null);
        obj.setBirthDate(new java.util.Date());
        obj.setFirstName("firstName_" + index);
        obj.setGender(null);
        obj.setId(null);
        obj.setLastName("lastName_" + index);
        obj.setParente(parentDataOnDemand.getRandomParent());
        return obj;
    }
    
    public Kid KidDataOnDemand.getSpecificKid(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Kid obj = data.get(index);
        return Kid.findKid(obj.get_id());
    }
    
    public Kid KidDataOnDemand.getRandomKid() {
        init();
        Kid obj = data.get(rnd.nextInt(data.size()));
        return Kid.findKid(obj.get_id());
    }
    
    public boolean KidDataOnDemand.modifyKid(Kid obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void KidDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = com.liwalla.domain.Kid.findKidEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Kid' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<com.liwalla.domain.Kid>();
        for (int i = 0; i < 10; i++) {
            com.liwalla.domain.Kid obj = getNewTransientKid(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
